// 链接服务器
wx.cloud.init({
  env: 't1-4gjkpt1t1e37bab9', //填上你的云开发环境id
  traceUser: true,
})
const db = wx.cloud.database();

var bh_id
var oldName = "";
var oldGender = "";
var phone = "";
var oldIdCard = "";
var oldImg;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: "",
    gender: ["男", "女"],
    genderId: "性别未选择",
    img: "",

  },

  uploadimg: function () {
    var that = this;
    wx.chooseImage({ //从本地相册选择图片或使用相机拍照
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res.tempFilePaths[0]);
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0], //选择图片返回的相对路径
          encoding: 'base64', //编码格式
          success: res1 => {
            //成功的回调
            if (res1.data.length > 500000) {
              wx.showModal({
                title: "图片太大", // 提示的标题
                content: "您上传的图片超过600KB，本站暂时未有自动压缩功能，可以自行发QQ或截屏，来进行人工压缩，感谢您的配合！",
                showCancel: true, // 是否显示取消按钮，默认true
                cancelColor: "#000000", // 取消按钮的文字颜色，必须是16进制格式的颜色字符串
                confirmText: "确定", // 确认按钮的文字，最多4个字符
                confirmColor: "#576B95", // 确认按钮的文字颜色，必须是 16 进制格式的颜色字符串
                complete: function () {}
              })
            } else {
              var base64Image = res1.data.replace(/[\r\n]/g, '') // 后台返回的base64数据
              var imgData = base64Image.replace(/[\r\n]/g, '') // 将回车换行换为空字符''
              that.setData({
                //前台显示
                img: res.tempFilePaths
              })
            }
          }
        })
      }
    })
  },
  time: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(id) {
    bh_id = id.id;
  },
  //预订按钮
  name(name) {
    oldName = name.detail.value
  },
  gender(gender) {
    oldGender = this.data.gender[gender.detail.value];
    this.setData({
      genderId: oldGender
    })
  },
  phone(ph) {
    phone = ph.detail.value;
  },
  idCard(idCard) {
    oldIdCard = idCard.detail.value;
  },
  okButton() {
    if (oldName == "") {
      wx.showToast({
        icon: 'error',
        title: '请输入姓名'
      })
    } else if (oldGender == "") {
      wx.showToast({
        icon: 'error',
        title: '请选择性别'
      })
    } else if (phone == "" || phone.length != 11) {
      wx.showToast({
        icon: 'error',
        title: '请输入联系电话'
      })
    } else if (oldIdCard == "" || oldIdCard.length != 18) {
      wx.showToast({
        icon: 'error',
        title: '请输入身份证号'
      })
    } else if (this.data.img == "") {
      wx.showToast({
        icon: 'error',
        title: '请选择照片'
      })
    } else if (this.data.time == "") {
      wx.showToast({
        icon: 'error',
        title: '请选择入住时间'
      })
    } else {
      //上传照片
      oldImg = 'oldImg/' + bh_id + " " + oldName + " " + phone + " " + oldIdCard + ".jpg";
      console.log(this.data.img[0]);
      wx.cloud.uploadFile({
        // 指定上传到的云路径
        cloudPath: oldImg,
        // 指定要上传的文件的小程序临时文件路径
        filePath: this.data.img[0],
        success: res => {
          console.log('上传成功', res.fileID)
        }
      })
      //查找身份证是否已上传
      db.collection('beadhouseOrder').where({
        idCard: oldIdCard,
      }).get({
        success: num => {
          var num = num.data.length;
          console.log(num)
          if (num > 0) {
            //存在就更改
            console.log("存在")
            db.collection('beadhouseOrder').where({
              idCard: oldIdCard,
            }).update({
              data: {
                id: bh_id,
                name: oldName,
                gender: oldGender,
                phone: phone,
                time: this.data.time,
                idCard: oldIdCard,
                img: "cloud://t1-4gjkpt1t1e37bab9.7431-t1-4gjkpt1t1e37bab9-1329722499/" + oldImg
              }
            })
          } else {
            //不存在就添加
            console.log("不存在")
            db.collection('beadhouseOrder').add({
              // data 字段表示需新增的 JSON 数据
              data: {
                id: bh_id,
                name: oldName,
                gender: oldGender,
                phone: phone,
                time: this.data.time,
                idCard: oldIdCard,
                img: "cloud://t1-4gjkpt1t1e37bab9.7431-t1-4gjkpt1t1e37bab9-1329722499/" + oldImg
              }
            })

          }

        }
      })

      wx.showToast({
        icon: 'success',
        title: '预定成功',
        duration: 1000
      })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1,
        })
      }, 1000);

    }
  }
})